<?php
class userHelper
{
	/*
	 * this returns all the usernames from the database
	 * the function is to be used in creating a new account, so no one 
	 * can create an account with a duplicate username
	 */
	static function getAllUsernames()
	{
		$DB = database::getDB();
		
		$allUsernames = array();
		$sql = "select USERNAME from users";
		$result = mysqli_query($DB, $sql);
		while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
			$allUsernames[] = $row['USERNAME'];
			
		mysqli_free_result($result);
		mysqli_close($DB);
		
		//echo '<pre>'.print_r($allUsernames, true).'</pre>';
		
		return $allUsernames;
	}
	
	/*
	 * retreives all usernames, ids, and types
	 * returns array
	 * $allUsers = array(
	 * 		[USER_TYPE_ID] = array(		//id of the type 1=general, etc
	 * 			[ID] => name			//id and name of the user
	 *		)
	 * )
	 */
	static function getAllUsersWithType()
	{
		$DB = database::getDB();
		
		$allUsers = array();
		$sql = "select ID, USERNAME, USER_TYPE_ID from users order by USER_TYPE_ID, USERNAME";
		
		$result = mysqli_query($DB, $sql);
		while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
			$allUsers[$row['USER_TYPE_ID']][$row['ID']] = $row['USERNAME'];
			
		mysqli_free_result($result);
		mysqli_close($DB);
		
		//echo '<pre>'.print_r($allUsers, true).'</pre>'; die();
		
		return $allUsers;
	}
	
	/*
	 * updates the user's types in the database
	 * takes an array with the users needing to be updated
	 * should be sanitized and in format:
	 * 		$allUsers = array( [USER_ID] => [USER_TYPE_ID] )
	 */
	static function updateAllUsersType($allUsers)
	{
		foreach($allUsers as $userID => $typeID)
			database::insertSQL("update users set USER_TYPE_ID = $typeID where ID = $userID");
	}
	
	/*
	 * retreives the id from the cookie
	 * else returns null if no cookie
	 */
	static function getUserID()
	{
		if(isset($_COOKIE["BuffBoardCookie"]))
			return mysql_real_escape_string($_COOKIE["BuffBoardCookie"]);
		else
			return null;
	}
}